
package business.customersubsystem;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import middleware.DbConfigProperties;
import middleware.dataaccess.DataAccessSubsystemFacade;
import middleware.exceptions.DatabaseException;
import middleware.externalinterfaces.DbConfigKey;
import middleware.externalinterfaces.DataAccessSubsystem;
import middleware.externalinterfaces.DbClass;
import business.externalinterfaces.Address;
import business.externalinterfaces.CustomerProfile;

class DbClassAddress implements DbClass {
	private static final Logger LOG = Logger.getLogger(DbClassAddress.class.getPackage().getName());
	private DataAccessSubsystem dataAccessSS = new DataAccessSubsystemFacade();
    private CustomerProfile custProfile;
    private Address address;
    
    private String queryType;
    private String query;
    
    private final String SAVE = "Save";
    
	//column names
    private final String STREET="street";
    private final String CITY = "city";
    private final String STATE = "state";
    private final String ZIP = "zip";
	
    public void saveAddress(CustomerProfile custProfile) throws DatabaseException {
        this.custProfile = custProfile;
        queryType = SAVE;
        dataAccessSS.saveWithinTransaction(this);
    }
    
    public void buildQuery() throws DatabaseException {
    	LOG.info("Query  for " + queryType + ": " + query);
        if(queryType.equals(SAVE)){
            buildSaveNewAddrQuery();
        }
        
        
    }
    
    Address getAddress() {
        return address;
    }
   
	
        
    void setAddress(Address addr){
        address = addr;
    }
    void buildReadCustNameQuery() {
        query = "SELECT fname, lname " +
        "FROM Customer " +
        "WHERE custid = " + custProfile.getCustId();        
    }
	
    void buildSaveNewAddrQuery() throws DatabaseException {
        query = "INSERT into altshipaddress " +
        		"(addressid,custid,street,city,state,zip) " +
        		"VALUES(NULL," +
        				  custProfile.getCustId() + ",'" +
        				  address.getStreet1()+"','" +
        				  address.getCity() + "','" +
        				  address.getState() + "','" +
        				  address.getZip() + "')";
    }
    
    public String getDbUrl() {
    	DbConfigProperties props = new DbConfigProperties();	
    	return props.getProperty(DbConfigKey.ACCOUNT_DB_URL.getVal());
        
    }
    public String getQuery() {
        return query;
        
    }
    
	
	
    /* used only when we read from the database
     */
    public void populateEntity(ResultSet rs) throws DatabaseException {
        	        	
    }
 
}
